package controllers;

import java.util.Date;
import java.util.List;

import models.CoreTable;
import models.TableRelation;
import models.User;

import org.apache.commons.lang.StringUtils;

import play.data.validation.Required;
import utils.Paged;

public class CoreTableController extends BaseController{
    
    private static String craeteQueryHql(String name, Long maintainerId) {
        StringBuilder hql = new StringBuilder(" 1 = 1 ");
        if (!StringUtils.isBlank(name)) {
            hql.append(" and name like '%").append(name).append("%' ");
        }
        
        if (maintainerId != null) {
            hql.append(" and maintainer.id = '").append(maintainerId).append("'");
        }
        return hql.toString();
    }
    
    public static void list(String name, Long maintainerId,int currentPage) {
        String hql = craeteQueryHql(name, maintainerId);
        currentPage = getCurrentPage(currentPage);
        int count = new Long(CoreTable.count(hql)).intValue();
        List<CoreTable> tables = CoreTable.find(hql).from((currentPage - 1) * PAGE_SIZE).fetch(PAGE_SIZE);
        Paged<CoreTable> paged = new Paged<CoreTable>(tables, currentPage, PAGE_SIZE, count);
        List<User> maintainers = User.findActiveUsers();
        render("/coretable/list.html", paged, maintainers, maintainerId, name);
    }
    
    
    public static void save(@Required(message = "coretable.name.require") String name, 
                            @Required(message = "coretable.maintainer.require") Long maintainerId,
                            String description) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(null);
        }
        
        CoreTable table = new CoreTable();
        table = toSetCoreTableValue(table, name, maintainerId, description);
        table.createDate = new Date();
        table.save();
        form(table.id);
    }
    
    public static void update(long tableId, 
                                @Required(message = "coretable.name.require") String name, 
                                @Required(message = "coretable.maintainer.require") Long maintainerId,
                                String description) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(tableId);
        }
        CoreTable exist = CoreTable.findById(tableId);
        if (!isExistTable(exist)) {
            notFound();
        }
        toSetCoreTableValue(exist, name, maintainerId, description).save();
        form(tableId);
    }
    
    public static void delete(long tableId) {
        CoreTable exist = CoreTable.findById(tableId);
        if (!isExistTable(exist)) {
            notFound();
        }
        exist.isDelete = true;
        exist.save();
    }
    
    private static CoreTable toSetCoreTableValue(CoreTable exist, String name, Long maintainerId, String description) {
        exist.name = name;
        User maintainer = getUser(maintainerId);
        if (!isExistUser(maintainer)) {
            notFound();
        }
        exist.description = description;
        exist.updateDate = new Date();
        exist.maintainer = maintainer;
        return exist;
    }

    public static void form(Long tableId) {
        List<User> maintainers = User.findActiveUsers();
        if (tableId == null) {
            render("/coretable/form.html", maintainers);
        }
        
        CoreTable coreTable = CoreTable.findById(tableId);
        if (coreTable == null) {
            notFound();
        }
        
        render("/coretable/form.html", maintainers, coreTable);
    }
    
    
    private static boolean isExistTable(CoreTable table) {
        return null != table;
    }
    
    public static void delete (Long tableId) {
        CoreTable.delete("id", tableId);
        list(null, null, FIRST_PAGE);
    }
}
